package puzzle.slilin2.view.text;

import java.io.IOException;

import puzzle.common.point.Pnt;
import puzzle.slilin2.data.SlilinBoard;
import puzzle.slilin2.data.SlilinPntHelper;
import puzzle.slilin2.data.LineCellState;
import puzzle.slilin2.data.NumberCellState;

public class TextCellFormatter {
	private Appendable m_out;
	
	public TextCellFormatter(Appendable out) {
		m_out = out;
	}
	
	public void format(SlilinBoard board, Pnt p) throws IOException {
		switch (SlilinPntHelper.getCellType(p)) {
		case DOT:
			formatDot(p);
			break;
		case HLINE:
			formatHLine(p, board.getLineCellState(p));
			break;
		case VLINE:
			formatVLine(p, board.getLineCellState(p));
			break;
		case NUMBER:
			formatNumber(board.getNumber(p), p);
			break;
		}
	}

	private void formatNumber(int number, Pnt p) throws IOException {
		if (number == NumberCellState.EMPTY) {
			m_out.append(" ");
		} else {
			m_out.append(String.valueOf(number));
		}
	}

	private void formatLine(char lineChar, Pnt p, LineCellState lineCellState) throws IOException {
		if (lineCellState.isForbidden()) {
			m_out.append("X");
		} else if (lineCellState.exists()) {
			m_out.append(lineChar);
		} else {
			m_out.append(" ");
		}
	}
	
	private void formatVLine(Pnt p, LineCellState lineCellState) throws IOException {
		formatLine('|', p, lineCellState);
	}

	private void formatHLine(Pnt p, LineCellState lineCellState) throws IOException {
		formatLine('-', p, lineCellState);
	}

	private void formatDot(Pnt p) throws IOException {
		m_out.append("+");
	}
	
}
